﻿using System;
using System.Reflection.Emit;

namespace TypeBuilderFx.Core
{
    /// <summary>Represents a variable, implementing <see cref="IVariable"/>.</summary>
    public class LocalVariable : IVariable
    {
        private readonly ILGeneratorExtender _ilGenerator;
        private readonly LocalBuilder _localVariable;

        /// <summary>Construct a local variable from an il-generator and the local builder representing the variable.</summary>
        /// <param name="ilGenerator"></param>
        /// <param name="localVariable"></param>
        public LocalVariable(ILGeneratorExtender ilGenerator, LocalBuilder localVariable)
        {
            _ilGenerator = ilGenerator;
            _localVariable = localVariable;
        }

        #region IVariable Members
        Type IVariable.Type
        {
            get { return _localVariable.LocalType; }
        }

        void IVariable.StoreIn()
        {
            _ilGenerator.StoreStackInVariable(_localVariable);
        }

        void IVariable.StackOut()
        {
            _ilGenerator.StackVariable(_localVariable);
        }
        #endregion
    }
}